package alva.com.getcamera.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;

import alva.com.getcamera.R;
import alva.com.getcamera.bean.ImageBean;

/**
 * Created by Administrator on 2016/11/10 0010.
 */
public class PicRecyclerViewAdapter extends RecyclerView.Adapter<PicRecyclerViewAdapter.ViewHolder> implements View.OnClickListener, View.OnLongClickListener {

    private Context context;
    private ArrayList<ImageBean> imageList;

    private OnChildClickListener onChildClickListener;
    private OnChildLongClickListener onChildLongClickListener;
    private RecyclerView mRecyclerView;

    public PicRecyclerViewAdapter(Context context, ArrayList<ImageBean> imageList) {
        this.context = context;
        this.imageList = imageList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.pic_recyclerview_item, parent, false);
        view.setOnClickListener(this);
        view.setOnLongClickListener(this);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ImageBean imageBean = imageList.get(position);
        holder.pic.setImageBitmap(getThumbnailImage(imageBean.getImagePath(),200,200));
    }

    @Override
    public int getItemCount() {
        return imageList == null ? 0 : imageList.size();
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.mRecyclerView = recyclerView;
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        this.mRecyclerView = null;
    }

    @Override
    public void onClick(View view) {
        if (onChildClickListener != null) {
            int position = mRecyclerView.getChildAdapterPosition(view);
            onChildClickListener.onChildClick(mRecyclerView, view, position, getItemId(position));
        }
    }

    public void setOnChildClickListener(OnChildClickListener onChildClickListener) {
        this.onChildClickListener = onChildClickListener;
    }

    public interface OnChildClickListener {
        void onChildClick(RecyclerView parent, View view, int position, long id);
    }

    @Override
    public boolean onLongClick(View v) {
        if (onChildLongClickListener!=null){
            int position=mRecyclerView.getChildAdapterPosition(v);
            String imagePath = imageList.get(position).getImagePath();
            onChildLongClickListener.onChildLongClick(mRecyclerView,v,position,getItemId(position),imagePath);
        }
        return true;
    }

    public void setOnChildLongClickListener(OnChildLongClickListener onChildLongClickListener) {
        this.onChildLongClickListener = onChildLongClickListener;
    }

    public interface OnChildLongClickListener{
        void onChildLongClick(RecyclerView parent,View view,int position,long id,String filePath);
    }

    public void addAll(ArrayList<ImageBean> list) {
        imageList.addAll(list);
        notifyDataSetChanged();
    }

    public void add(ImageBean imageBean) {
        imageList.add(imageBean);
        notifyDataSetChanged();
    }

    public void add(int i, ImageBean imageBean) {
        imageList.add(i, imageBean);
        notifyDataSetChanged();
    }

    public void remove(int i){
        imageList.remove(i);
        notifyDataSetChanged();
    }

    public void clear() {
        imageList.clear();
        notifyDataSetChanged();
    }

    private Bitmap getThumbnailImage(String imagePath, int width, int height) {
        Bitmap bitmap = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        //获取图片的宽高
        bitmap = BitmapFactory.decodeFile(imagePath, options);
        options.inJustDecodeBounds = false;
        int w = options.outWidth;
        int h = options.outHeight;
        int wRatio = w / width;
        int hRatio = h / height;
        int ratio = 1;
        if (wRatio < hRatio) {
            ratio = wRatio;
        } else {
            ratio = hRatio;
        }
        if (ratio <= 0) {
            ratio = 1;
        }
        options.inSampleSize = ratio;
        bitmap = BitmapFactory.decodeFile(imagePath, options);
        bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
        return bitmap;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView pic;

        public ViewHolder(View itemView) {
            super(itemView);
            pic = (ImageView) itemView.findViewById(R.id.pic_item);
        }
    }
}
